package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallOrderGoodsAccessoryMapper;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsAccessory;
import org.linlinjava.litemall.db.domain.LitemallOrderGoodsAccessoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

 

@Service
public class LitemallOrderGoodsAccessoryService extends MPJBaseServiceImpl<LitemallOrderGoodsAccessoryMapper,LitemallOrderGoodsAccessory>{

    @Resource
    private LitemallOrderGoodsAccessoryMapper litemallOrderGoodsAccessoryMapper;

    public int add(LitemallOrderGoodsAccessory litemallGoodsAccessory) {
        litemallGoodsAccessory.setAddTime(LocalDateTime.now());
        return litemallOrderGoodsAccessoryMapper.insertSelective(litemallGoodsAccessory);
    }

    public int deleteById(Integer id) {
        return litemallOrderGoodsAccessoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public int deleteByGoodsId(Integer goodsId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return litemallOrderGoodsAccessoryMapper.logicalDeleteByExample(example);
    }

    public int update(LitemallOrderGoodsAccessory litemallGoodsAccessory) {
        return litemallOrderGoodsAccessoryMapper.updateByPrimaryKeySelective(litemallGoodsAccessory);
    }


    public LitemallOrderGoodsAccessory findById(Integer id) {
        return litemallOrderGoodsAccessoryMapper.selectByPrimaryKey(id);
    }
    
    public List<LitemallOrderGoodsAccessory> findByGoodsId(Integer goodsId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andGoodsIdEqualTo(goodsId).andDeletedEqualTo(false);
        return litemallOrderGoodsAccessoryMapper.selectByExample(example);
    }


    public List<LitemallOrderGoodsAccessory> findByOrderGoodsId(Integer orderGoodsId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
        return litemallOrderGoodsAccessoryMapper.selectByExample(example);
    }
    
    public Long countByOrderGoodsId(Integer orderGoodsId) {
    	LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
    	example.or().andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
    	return litemallOrderGoodsAccessoryMapper.countByExample(example);
    }

    public List<LitemallOrderGoodsAccessory> findByOrderId(Integer orderId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andOrderIdEqualTo(orderId).andDeletedEqualTo(false);
        return litemallOrderGoodsAccessoryMapper.selectByExample(example);
    }
    
    public int deleteByOrderIdAndOrderGoodsId(Integer orderId,Integer orderGoodsId) {
    	LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
    	example.or().andOrderIdEqualTo(orderId).andOrderGoodsIdEqualTo(orderGoodsId).andDeletedEqualTo(false);
    	return litemallOrderGoodsAccessoryMapper.deleteByExample(example);
    }
    
    public int deleteByOrderId(Integer orderId) {
    	 LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
         example.or().andOrderIdEqualTo(orderId);
         return litemallOrderGoodsAccessoryMapper.deleteByExample(example);
    }

    public int logicDeleteByOrderId(Integer orderId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andOrderIdEqualTo(orderId);
        return litemallOrderGoodsAccessoryMapper.logicalDeleteByExample(example);
    }

    public List<LitemallOrderGoodsAccessory> querySelective(Integer page, Integer size, String sort, String order) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        //PageHelper.startPage(page, size);
        return litemallOrderGoodsAccessoryMapper.selectByExample(example);
    }
    
    public LitemallOrderGoodsAccessory findByOrderGoodsIdAndAccessoryIdAndNumber(Integer orderGoodsId, Integer accessoryId, Integer number) {
    	LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
    	example.or().andOrderGoodsIdEqualTo(orderGoodsId).andAccessoryIdEqualTo(accessoryId).andNumberEqualTo(number).andDeletedEqualTo(false);
    	return litemallOrderGoodsAccessoryMapper.selectOneByExample(example);
    }
    
    public boolean checkAccessoryIdExist(Integer accessoryId) {
    	LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
    	example.or().andAccessoryIdEqualTo(accessoryId).andDeletedEqualTo(false);
    	return litemallOrderGoodsAccessoryMapper.countByExample(example) != 0;
    }

    public LitemallOrderGoodsAccessory findByOrderGoodsIdAndAccessoryId(Integer orderGoodsId, Integer accessoryId) {
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
        example.or().andOrderGoodsIdEqualTo(orderGoodsId).andAccessoryIdEqualTo(accessoryId).andDeletedEqualTo(false);
        return litemallOrderGoodsAccessoryMapper.selectOneByExample(example);
    }

    public List<LitemallOrderGoodsAccessory> queryNotSelected(List<Integer> accessoryIds,Integer orderGoodsId){
        LitemallOrderGoodsAccessoryExample example = new LitemallOrderGoodsAccessoryExample();
       if(accessoryIds != null ){
           example.or().andOrderGoodsIdEqualTo(orderGoodsId).andAccessoryIdNotIn(accessoryIds);
       }else{
           example.or().andOrderGoodsIdEqualTo(orderGoodsId);
       }

        return litemallOrderGoodsAccessoryMapper.selectByExample(example);
    }

}
